/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package data;

import business.RegisterMember;
import business.ShowMember;
import java.sql.*;

/**
 *
 * @author User
 */
public class RegisterMemberDBase extends ConnectionManager {

    public static boolean cekMember(String email) {
        Statement st = null;
        ResultSet rs = null;
        boolean x = true;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url + dbname, user, password);
            st = conn.createStatement();
            rs = st.executeQuery("SELECT * FROM member where email= '" + email + "'");
            System.out.println("SELECT * FROM member where email= '" + email + "'");
            if (rs.next()) {
                x = false;

            } else {
                x = true;
            }
        } catch (Exception e) {
        }
        return x;
    }

    public static RegisterMember InsertMember(String nama, String jk, String email, String pass) {
        RegisterMember user = new RegisterMember();
        Statement st = null;
        ResultSet rs = null;

        try {

            st = conn.createStatement();
            
            st.executeUpdate("INSERT INTO member(nama_member,jenis_kelamin,email,password) VALUES ('" + nama + "','" + jk + "','" + email + "','" + pass+"')");
                   

            if (rs.next()) {
                user.setNama(rs.getString("nama"));
                user.setJk(rs.getString("jk"));
                user.setEmail(rs.getString("email"));
                user.setPass(rs.getString("pass"));
            }
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public  ShowMember ShowMember(String id) {
        ShowMember user = new ShowMember();

        Statement st = null;
        ResultSet rs = null;

        try {

            st = conn.createStatement();

            st.executeQuery("select * from member where id_member ='"+id+"'");

            if (rs.next()) {
                user.setId(rs.getString(1));
                user.setNama(rs.getString(2));
                user.setJk(rs.getString(3));
                user.setEmail(rs.getString(4));
                user.setRole(rs.getString(6));
                System.out.println("user.setNama(rs.getString(2));");
                System.out.println(rs.getString(2));
              
            }
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static RegisterMember UpdateMember(String id,String nama, String jk, String email, String role) {
        RegisterMember user = new RegisterMember();
        Statement st = null;
        ResultSet rs = null;

        try {

            st = conn.createStatement();

            st.executeUpdate("INSERT INTO member(nama_member,jenis_kelamin,email,password) VALUES ('" + nama + "','" + jk + "','" + email + "','" + role+"')");


            if (rs.next()) {
                user.setEmail(rs.getString("nama"));
                user.setPass(rs.getString("jk"));
                user.setEmail(rs.getString("email"));
                user.setPass(rs.getString("pass"));
            }
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
